<template>
  <div>
    <!-- 添加或修改OA 请假申请对话框 -->
    <el-drawer
      ref="drawer"
      :title="title"
      direction="rtl"
      :visible.sync="open"
      size="80%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; min-height: 90vh"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="projectName">
                <el-input
                  v-model="form.projectName"
                  disabled
                  placeholder="请输入项目名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号" prop="projectOn">
                <el-input
                  v-model="form.projectOn"
                  disabled
                  placeholder="请输入项目编号"
                  readonly
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属部门" prop="deptId">
                <treeselect
                  v-model="form.deptId"
                  :options="deptOptions"
                  disabled
                  :show-count="true"
                  placeholder="请选择归属部门"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="立项时间" prop="projectTime">
                <el-date-picker
                  clearable
                  style="width: 100%"
                  v-model="form.projectTime"
                  disabled
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择立项时间"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="applyUser">
                <el-select
                  v-model="form.applyUser"
                  disabled
                  placeholder="请选择协同人"
                  style="width: 100%"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="user in userList"
                    :key="user.userId"
                    :label="user.userName"
                    :value="user.userId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责人" prop="chargeUser">
                <el-select
                  v-model="form.chargeUser"
                  disabled
                  placeholder="请选择协同人"
                  style="width: 100%"
                  filterable
                  clearable
                  multiple
                >
                  <el-option
                    v-for="user in userList"
                    :key="user.userId"
                    :label="user.userName"
                    :value="user.userId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目性质" prop="nature">
                <el-radio-group v-model="form.nature">
                  <el-radio
                    v-for="dict in dict.type.crm_order_nature"
                    disabled
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签订单位" prop="laborContracts">
                <el-radio-group v-model="form.laborContracts">
                  <el-radio
                    v-for="dict in dict.type.crm_order_contracts"
                    disabled
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始日期" prop="startTime">
                <el-date-picker
                  clearable
                  style="width: 100%"
                  disabled
                  v-model="form.startTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束日期" prop="endTime">
                <el-date-picker
                  clearable
                  style="width: 100%"
                  disabled
                  v-model="form.endTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerId">
                <el-select
                  v-model="form.customerId"
                  disabled
                  placeholder="请选择客户名称"
                  style="width: 100%"
                  filterable
                >
                  <el-option
                    v-for="customer in customerList"
                    :key="customer.customerId"
                    :label="customer.customerName"
                    :value="customer.customerId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目金额" prop="projectSum">
                <el-input
                  v-model="form.projectSum"
                  disabled
                  placeholder="请输入项目金额"
                >
                  <template slot="append">{{
                    digitUppercase(form.projectSum)
                  }}</template></el-input
                >
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="预计利润" prop="profit">
                <el-input
                  v-model="form.profit"
                  placeholder="请输入预计利润"
                  readonly
                >
                  <template slot="append">{{
                    digitUppercase(form.profit)
                  }}</template></el-input
                >
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="利润率" prop="profitMargin">
                <el-input
                  v-model="form.profitMargin"
                  placeholder="请输入利润率"
                  readonly
                >
                  <template slot="append">%</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="付款方式" prop="paymentTime">
                <el-input
                  v-model="form.paymentTime"
                  disabled
                  placeholder="请输入付款方式"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-divider>项目预算成本费用支出</el-divider>
              <el-form-item label="费用明细" prop="details" class="details">
                <el-table :data="form.detailsList" style="width: 100%">
                  <el-table-column label="费用类型" prop="detailsName">
                  <template slot-scope="scope">
                      <el-select
                        v-model="scope.row.detailsType"
                        placeholder="请选择费用类型"
                        style="width: 100%"
                        :disabled="
                          scope.row.isRequired === '1' || scope.row.flag === '1'
                        "
                      >
                        <el-option
                          v-for="dict in dict.type.crm_fee_type"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </template>
                  </el-table-column>
                  <el-table-column label="费用名称" prop="detailsName">
                    <template slot-scope="scope">
                      <el-input
                        :readonly="scope.row.flag === '1'"
                        v-model="scope.row.detailsName"
                        placeholder="请输入费用名称"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column label="费用描述" prop="detailsDesc">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.detailsDesc"
                        placeholder="请输入费用详情描述"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column label="预算合计" prop="detailsAmount">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.detailsAmount"
                        placeholder="请输入预算合计"
                        type="number"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column label="备注" prop="remark">
                    <template slot-scope="scope">
                      <el-input
                        v-model="scope.row.remark"
                        placeholder="请输入备注"
                        disabled
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="是否存在明细"
                    prop="isRequired"
                    width="100"
                  >
                    <template slot-scope="scope">
                      <el-checkbox
                        :value="scope.row.isRequired === '1'"
                        disabled
                        >无</el-checkbox
                      >
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="费用总计" prop="aggregationCharge">
                <el-input
                  v-model="form.aggregationCharge"
                  placeholder="请输入费用总计"
                  disabled
                >
                  <template slot="append">{{
                    digitUppercase(form.aggregationCharge)
                  }}</template></el-input
                >
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="管理层意见" prop="opinion">
                <el-input
                  v-model="form.opinion"
                  placeholder="请输入管理层意见"
                  disabled
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="附件" prop="url">
                <file-upload v-model="form.url" disabled/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-divider>审批通道</el-divider>
              <el-form-item label="电子签名" prop="signature">
                <div class="box">
                  <VueSignaturePad
                    width="100%"
                    height="250px"
                    ref="signaturePad"
                  />
                  <el-button @click="undo" type="text" class="box-button"
                    >清空签名</el-button
                  >
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="审批意见" prop="message">
                <el-input
                  v-model="form.message"
                  type="textarea"
                  placeholder="请输入审批意见"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row  v-if="!disabled">
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="handleBtn('PASS')"
                  >通过</el-button
                >
                <el-button @click="handleBtn('REJECT')">驳回</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { getOrderRegister, audit } from "@/api/crm/orderRegister";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";

import { deptTreeSelect } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { getUserListByStatus } from "@/api/system/user";

export default {
  name: "Approve",
  dicts: ["crm_order_nature", "crm_order_contracts", "crm_fee_type"],
  props: {
    /* 业务id */
    value: {
      type: String,
      default: "",
    },

    /* 实例id */
    taskId: {
      type: String,
      default: "",
    },

    /* 是否可以标编辑 */
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    Treeselect,
  },
  data() {
    return {
      deptOptions: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 指定跳转节点
      nodeCode: "",
      customerList: [],
      orderList: [],
      rules: {
        // signature: [
        //   {
        //     required: true,
        //     validator: this.validateDetails,
        //     trigger: "change",
        //   },
        // ],
      },
    };
  },
  created() {
    this.reset();
    this.getUserListByStatus();
    this.getDeptTree();
    this.getCrmCustomerAllNoPage()
    this.getOrderRegister();
  },
  mounted(){
    setTimeout(() => {
      let data = window.localStorage.getItem("签名_" + this.$store.getters.userId)
      if(data){
        this.$refs.signaturePad.fromDataURL(data);
      }
    }, 500)
  },
  methods: {
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    validateDetails(rule, value, callback) {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
      if (isEmpty) {
        callback(new Error("电子签名必须填写"));
      } else {
        callback();
      }
    },
    // 撤销操作
    undo() {
      this.$refs.signaturePad.clearSignature();
    },
    getCrmCustomerAllNoPage() {
      getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    async getOrderRegister() {
      await getOrderRegister(this.value).then((response) => {
        if (!response.data) {
          this.$modal.alertWarning("代办任务不存在");
        }
        this.form = response.data;
        this.form.chargeUser = this.form.chargeUser ? this.form.chargeUser.split(",") : null;
        this.title = this.form.createBy + " 项目立项申请";
        this.open = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        message: null,
        id: null,
        type: null,
        reason: null,
        startTime: null,
        endTime: null,
        day: null,
        instanceId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 审核通过按钮 */
    handleBtn(skipType) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.taskId = this.taskId;
          this.form.skipType = skipType;
          this.form.chargeUser = this.form.chargeUser?.join(",")
          const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
          this.form.signature = data;
          audit(this.form).then((response) => {
            this.$modal.msgSuccess("办理成功");
            window.localStorage.setItem("签名_" + this.$store.getters.userId, this.form.signature)
            this.open = false;
            this.$emit("refresh");
          });
        }
      });
    },
  },
};
</script>

<style scoped>
::v-deep .el-drawer__header {
  margin-bottom: 20px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .el-textarea.is-disabled .el-textarea__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .details .el-table--medium .el-table__cell {
  padding: 0;
}

::v-deep .details .el-input--medium {
  margin: 10px 10px 10px -10px;
}

.box {
  width: 100%;
  height: 250px;
  border: 1px solid #dcdfe6;
  position: relative;
}

.box-button {
  position: absolute;
  top: 215px;
  left: 93%;
}
</style>